//
//  MXTabViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/5/26.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "MXTabViewController.h"

#import "MXNavViewController.h"

#import "HomeViewController.h"
#import "InformationViewController.h"
#import "DiscoverViewController.h"
#import "LotteryViewController.h"
#import "MineViewController.h"

#import "LoginViewController.h"

#import "ForumViewController.h"
@interface MXTabViewController ()

@end

@implementation MXTabViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setDelegate:self];
    [self setupChildControllers];
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    //这句表示取消tabBar的透明效果
    [UITabBar appearance].translucent = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupChildControllers{
    
    HomeViewController *oneVC = [[HomeViewController alloc] init];
    oneVC.navigationItem.title = oneText;
    [self addChildVCWith:oneVC title:oneText nmlImgName:@"shouye" selImgName:@"shouye-"];
    
    
    InformationViewController *twoVC = [[InformationViewController alloc] init];
    twoVC.navigationItem.title = twoText;
    [self addChildVCWith:twoVC title:twoText nmlImgName:@"zixun" selImgName: @"zixun-"];
    
    
    
    DiscoverViewController *threeVC = [[DiscoverViewController alloc] init];
    threeVC.navigationItem.title = ThreeText;
    [self addChildVCWith:threeVC title:ThreeText nmlImgName:@"faxian" selImgName: @"faxian-"];
    
    
    ForumViewController *fourVc = [[ForumViewController alloc] init];
    fourVc.navigationItem.title = FourText;
    [self addChildVCWith:fourVc title:FourText nmlImgName:@"kaijiang" selImgName: @"kaijiang-"];
    
    
    
    MineViewController *fiveVc = [[MineViewController alloc] init];
    fiveVc.navigationItem.title = FiveText;
    [self addChildVCWith:fiveVc title:FiveText nmlImgName:@"wode" selImgName: @"wode-"];
    
    
    
    // 避免顶部出现20像素
//    threeVC.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)addChildVCWith:(UIViewController *)vc title:(NSString *)title nmlImgName:(NSString *)nmlImgName selImgName:(NSString *)selImgName {
    
    MXNavViewController *nav = [[MXNavViewController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    //设置标题
    nav.tabBarItem.title = title;
    
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor grayColor], NSForegroundColorAttributeName,
                                            nil] forState:UIControlStateNormal];
    
    
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            DEF_COLOR_MAINCOLOR, NSForegroundColorAttributeName,
                                            nil] forState:UIControlStateSelected];
    
    //设置普通状态图片
    nav.tabBarItem.image = [[UIImage imageNamed:nmlImgName]
                            imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selImg = [UIImage imageNamed:selImgName];
    selImg = [selImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = selImg;
    
    nav.tabBarItem.imageInsets =UIEdgeInsetsMake(0,0, 0,0);
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (tabBarController.selectedIndex == 4) {
        
        [RACObserve(UserInfoShare, isLogin) subscribeNext:^(id x) {
            
            NSLog(@"xxxxxx %ld",(long)[x integerValue]);
            
            if ([x integerValue] == 0) {
                
                LoginViewController * login = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:login];
                [self presentViewController:navc animated:YES completion:^{
                }];
                
            }
        }];
    }
}

@end
